package top.hekun.study.core.visitor.method.complex;

import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.MethodVisitor;


/**
 * 清除 +0 无效语句〉
 *
 * @Author: 贺坤
 * @Date: 2023/5/12 21:25
 */
public class MyClassVisitorComplexCode1 extends ClassVisitor {
    private int apiVersion;
    private String thisClassName = this.getClass().getSimpleName();

    public MyClassVisitorComplexCode1(int apiVersion, ClassVisitor classVisitor) {
        super(apiVersion, classVisitor);
        this.apiVersion = apiVersion;
    }

    /**
     * 目标 将
     * int d = c + 0;
     * 改为
     * int d = c
     *  本质 去掉
     *  iconst_0
     *  iadd
     *  两个命令;
     * @param access
     * @param name
     * @param descriptor
     * @param signature
     * @param exceptions
     * @return
     */
    @Override
    public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
        System.out.println(thisClassName + " -- " + name + " -- " + descriptor);
        MethodVisitor visitor = super.visitMethod(access, name, descriptor, signature, exceptions);
        if (visitor != null && "add1".equals(name) && "(II)I".equals(descriptor)) {
            return new MyMethodVisitor1(apiVersion, visitor);
        }
        return visitor;
    }

}
